package org.easy.web.service.file.impl;

import org.easy.file.model.FileModel;
import org.easy.file.monitor.FileFactory;
import org.easy.tool.res.Result;
import org.easy.web.service.file.FileMonitorService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 文件监控实现类
 * @Author: wxl
 * @CreateTime: 2022/12/28 9:03
 */
@Service
public class FileMonitorServiceImpl implements FileMonitorService {

    private final FileFactory fileFactory;

    public FileMonitorServiceImpl(FileFactory fileFactory) {
        this.fileFactory = fileFactory;
    }

    /**
     * 开启监控任务
     *
     * @param filePath 文件路径
     * @return Result<?>
     */
    @Override
    public Result<?> startFileMonitor(String filePath) {

        try {
            fileFactory.startTask(filePath);
            return Result.success();
        } catch (Exception e) {
            return Result.failed();
        }
    }

    /**
     * 根据文件路径停止任务
     *
     * @param filePath 文件路径
     * @return Result<Boolean>
     */
    @Override
    public Result<Boolean> stopFileMonitor(String filePath) {
        return Result.success(fileFactory.stopTask0(filePath));
    }

    /**
     * 获取所有的任务列表
     *
     * @return Result<List < FileModel>>
     */
    @Override
    public Result<List<FileModel>> queryFileTaskList() {
        return Result.success(fileFactory.getFileTasks());
    }
}
